package core

import "time"

//写es的log格式
type Mlog struct {
	Level      string
	ServerIp   string
	ClientIp   string
	LogTime    string
	TraceId    string
	Method     string
	Path       string
	Uri        string
	Req        string
	StatusCode int
	Cost       float64
	Msg        string
}

type Response struct {
	Time   int64       `json:"time"`
	Status int         `json:"status"`
	Msg    string      `json:"msg"`
	Data   interface{} `json:"data"`
	Node   string      `josn:"node"`
}

type WatchCallback func(string, string, string)

const (
	Success = iota + 10000
	Failure
	MethodError
	MdError
	ParamError
	SystemError
	TokenError
	TooFast
	SysError = 0

	DefaultRate = 100000 //默认qps
)

var (
	ParamsMap = map[string]string{
		"AccessKey": "AccessKey",
		"SourceId":  "SourceId",
		"S":         "S",
		"TS":        "TS",
	}
	code2message = map[int]string{
		SysError:    "系统错误",
		Success:     "操作成功",
		Failure:     "操作失败，请稍后再试",
		ParamError:  "参数错误",
		SystemError: "系统异常",
		TokenError:  "签名错误",
		TooFast:     "操作太快啦",
		MethodError: "method无权限",
		MdError:     "幂等验证失败",
	}
)

func ResponseOk(data interface{}) (res *Response) {
	res = &Response{
		Time:   time.Now().Unix(),
		Status: Success,
		Msg:    code2message[Success],
		Data:   data,
		Node:   GetIp(),
	}
	return

}

//如果code为自定义，那么params[0]就是msg
func ResponseErr(code int, params ...string) (res *Response) {
	msg, ok := code2message[code]
	if !ok || len(params) == 1 {
		msg = params[0]
	}
	res = &Response{
		Time:   time.Now().Unix(),
		Status: code,
		Msg:    msg,
		Data:   []string{},
		Node:   GetIp(),
	}
	return

}
